package cn.ad.server.web.servlet;


import cn.ad.server.common.utils.EncodeUtils;
import cn.ad.server.common.utils.SpringContextUtils;
import cn.ad.server.web.log.IAdServerLogger;
import cn.ad.server.web.servlet.v2.RestPathFilter;
import cn.ad.server.web.spring.service.actions.command.ClickAndImpressionCommand;
import cn.ad.server.web.spring.service.actions.context.STSAdProcessor;
import cn.ad.server.biz.internal.AdService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dongbin.yu
 * @from 2016-05-18
 * @since V1.0
 * @deprecated 原广告请求处理Servlet框架将不推荐使用，统一采用v2包下的处理方式，请求入口参考{@link RestPathFilter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)}
 */
@Deprecated
public abstract class AbstractAdServlet extends HttpServlet {
//
//	protected final ObjectMapper objectMapper = new ObjectMapper();
//	
//	protected AdService adService;
//
//	protected IAdServerLogger loggerService;
//
//	protected STSAdProcessor adProcessor;
//
//	@Override
//	public void init() throws ServletException {
//		// 配置null过滤掉
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//		// 忽略与POJO中不匹配的属性
//		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//		objectMapper.enable(SerializationFeature.CLOSE_CLOSEABLE);
//		adService = SpringContextUtils.getBean(AdService.class);
//		loggerService = SpringContextUtils.getBean(IAdServerLogger.class);
//		adProcessor = SpringContextUtils.getBean(STSAdProcessor.class);
//	}
//
//	@Override
//	protected void service(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		doService(request, response);
//	}
//
//	abstract void doService(HttpServletRequest request, HttpServletResponse response);
//

//
//<<<<<<< HEAD
//=======
    protected final ObjectMapper objectMapper = new ObjectMapper();

    protected AdService adService;

    protected IAdServerLogger loggerService;

    protected STSAdProcessor adProcessor;

    @Override
    public void init() throws ServletException {
        //配置null过滤掉
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.enable(SerializationFeature.CLOSE_CLOSEABLE);
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		
        adService = SpringContextUtils.getBean(AdService.class);
        loggerService = SpringContextUtils.getBean(IAdServerLogger.class);

        adProcessor = SpringContextUtils.getBean(STSAdProcessor.class);

    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doService(request, response);
    }

    abstract void doService(HttpServletRequest request, HttpServletResponse response);
    
	public static boolean covertGDTParam(ClickAndImpressionCommand clickCommand) {
		if (StringUtils.isEmpty(clickCommand.getSource_base64())
				&& StringUtils.isEmpty(clickCommand.getSource_urlencode()))
			return false;
		Map<String, String> map = new HashMap<String, String>();
		String decode = "";
		if (!StringUtils.isEmpty(clickCommand.getSource_base64()))
			decode = EncodeUtils.base64Decode(clickCommand.getSource_base64());
		if (!StringUtils.isEmpty(clickCommand.getSource_urlencode()))
			decode = EncodeUtils.URLDecode(clickCommand.getSource_urlencode());
		String[] array = decode.split("&");
		if (ArrayUtils.isEmpty(array))
			return false;
		for (String a : array) {
			String[] m = a.split("=");
			map.put(m[0], m[1]);
		}
		clickCommand.setCid(map.get("cid"));
		clickCommand.setGuid(map.get("guid"));
		clickCommand.setSid(map.get("sid"));
		clickCommand.setMid(map.get("mid"));
		return true;
	}
    
}
